/**
 *
 * @author 
 *
 */
class ChangePage extends egret.Sprite{
    
    public static PAGE_WIDTH:number = 680;
    public static PAGE_HEIGHT:number = 1036;
    
    private up_left_point:egret.Point;
    private up_right_point: egret.Point;
    private bottom_left_point:egret.Point;
    private bottom_right_point: egret.Point;
    
	public constructor() {
    	super();
    	this.init();
	}
	
	private bottom1Point:egret.Point;
	private topPoint:egret.Point;
    private up1Point: egret.Point;
	private controlPoint:egret.Point;
	
	private bottom2Point:egret.Point;
	private up2Point:egret.Point;
	
	private maskUp:egret.Shape;
	private maskDown:egret.Shape;
	
	private down:egret.Sprite;
	private up:egret.Sprite;
	
	private dis:egret.Point;
	
	private trigger:egret.Shape;
	
	private leftPages:egret.DisplayObject[];
	private rightPages:egret.DisplayObject[];
	
	/**true往左翻，false往右翻*/
	private tapDir:boolean = false;
	
	public init():void {
    	  this.leftPages = [];
    	  this.rightPages = [];
	    
    	  this.x = 100;
    	  this.y = 100;
    	  
        this.down = new egret.Sprite;
        this.down.x = this.x;
        this.down.y = this.y;
        this.addChild(this.down);
        
        var bmp:egret.Bitmap = new egret.Bitmap;
        bmp.texture = RES.getRes("p1_png");
        this.down.addChild(bmp);
        
        ChangePage.PAGE_WIDTH = bmp.width;
        ChangePage.PAGE_HEIGHT = bmp.height;
        
        var mw = Math.sqrt(bmp.width*bmp.width+bmp.height*bmp.height);
        var mh = mw;
        this.dis = new egret.Point;
        this.dis.x = (mw - bmp.width);
        this.dis.y = (mh - bmp.height);
        this.trigger = new egret.Shape;
        this.trigger.graphics.beginFill(0,0.1);
        this.trigger.graphics.drawRect(0,0,mw * 2 + this.dis.x/2, mh+this.dis.y);
        this.trigger.graphics.endFill();
        this.trigger.touchEnabled = true;
        this.trigger.x = this.down.x - this.dis.x;
        this.trigger.y = this.down.y - this.dis.y;
        this.addChild(this.trigger);
        
	    this.controlPoint = new egret.Point;
	    this.controlPoint.x = 0;
	    this.controlPoint.y = ChangePage.PAGE_HEIGHT;
	    
        this.topPoint = new egret.Point;
	    this.topPoint.x = this.controlPoint.x;
	    this.topPoint.y = this.controlPoint.y;
	    
        this.up1Point = new egret.Point;
	    this.up1Point.x = 0;
	    this.up1Point.y = 0;
	    
        this.bottom1Point = new egret.Point;
	    this.bottom1Point.x = 0;
	    this.bottom1Point.y = this.controlPoint.y;
	    
        this.up2Point = new egret.Point;
	    this.up2Point.x = ChangePage.PAGE_WIDTH;
	    this.up2Point.y = 0;
	    
        this.bottom2Point = new egret.Point;
        this.bottom2Point.x = ChangePage.PAGE_WIDTH;
        this.bottom2Point.y = ChangePage.PAGE_HEIGHT;
	    
	    this.up = new egret.Sprite;
	    this.addChild(this.up);
	    
	    this.maskDown = new egret.Shape;
	    this.maskDown.graphics.beginFill(0,1);
	    this.maskDown.graphics.moveTo(this.bottom1Point.x,this.bottom1Point.y);
        this.maskDown.graphics.lineTo(this.topPoint.x,this.topPoint.y);
        this.maskDown.graphics.lineTo(this.up1Point.x,this.up1Point.y);
        this.maskDown.graphics.lineTo(this.up2Point.x,this.up2Point.y);
        this.maskDown.graphics.lineTo(this.bottom2Point.x,this.bottom2Point.y);
        this.maskDown.graphics.endFill();
        this.down.addChild(this.maskDown);
        this.down.mask = this.maskDown;
	    
        bmp = new egret.Bitmap();
        bmp.texture = RES.getRes("p2_png");
        this.up.addChild(bmp);
        this.up.anchorOffsetX = bmp.width;
        this.up.anchorOffsetY = bmp.height;
	    
	    this.maskUp = new egret.Shape;
	    this.maskUp.graphics.beginFill(0,1);
	    this.maskUp.graphics.lineStyle(2,0x00ff00, 1);
	    this.maskUp.graphics.moveTo(this.bottom1Point.x,this.bottom1Point.y);
	    this.maskUp.graphics.lineTo(this.topPoint.x,this.topPoint.y);
	    this.maskUp.graphics.lineTo(this.controlPoint.x,this.controlPoint.y);
	    this.maskUp.graphics.endFill();
	    this.up.addChild(this.maskUp);
	    this.up.mask = this.maskUp;
	    
	    this.up_left_point = new egret.Point;
	    this.up_left_point.x = 0;
	    this.up_left_point.y = 0;
	    
	    this.up_right_point = new egret.Point;
	    this.up_right_point.x = ChangePage.PAGE_WIDTH;
	    this.up_right_point.y = 0;
	    
	    this.bottom_left_point = new egret.Point;
	    this.bottom_left_point.x = 0;
	    this.bottom_left_point.y = ChangePage.PAGE_HEIGHT;
	    
	    this.bottom_right_point = new egret.Point;
	    this.bottom_right_point.x = ChangePage.PAGE_WIDTH;
	    this.bottom_right_point.y = ChangePage.PAGE_HEIGHT;
	    
        this.trigger.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBegin,this);
        this.trigger.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.onTouchMove,this);
        this.trigger.addEventListener(egret.TouchEvent.TOUCH_END,this.onTouchEnd,this);
        
        this.tmpp = new egret.Point;
	}
	
	
	
	
	private onTouchBegin(e:egret.TouchEvent):void {
    	console.log("tmpp.x="+this.tmpp.x+",tmpp.y="+this.tmpp.y)
        if(e.localX > ChangePage.PAGE_WIDTH+this.dis.x) {
            this.tapDir = true;
            this.up.x = ChangePage.PAGE_WIDTH + 100;
            this.down.x = this.up.x;
            this.up.anchorOffsetX = 0;
            this.up.anchorOffsetY = this.up.height;
            
            this.calcControlPoint_Right(e.localX - ChangePage.PAGE_WIDTH,e.localY);
        } else {
            this.up.x = this.down.x = 100;
            this.up.anchorOffsetX = this.up.width;
            this.up.anchorOffsetY = this.up.height;
            this.tapDir = false;

            this.calcControlPoint_Left(e.localX,e.localY);
        }
	}
	
	private onTouchMove(e:egret.TouchEvent):void {
        if(!this.tapDir) {
            this.calcControlPoint_Left(e.localX,e.localY);
        } else {
            this.calcControlPoint_Right(e.localX - ChangePage.PAGE_WIDTH,e.localY);
        }
	}
	
	private onTouchEnd(e:egret.TouchEvent):void {
    	console.log("onTouchEnd")
        if(!this.tapDir) {
            this.tmpp.x = 0;
            this.tmpp.y = 0;
        } else {
            this.tmpp.x = 2 * ChangePage.PAGE_WIDTH;
            this.tmpp.y = ChangePage.PAGE_HEIGHT;
        }
	}
	private tmpp:egret.Point;
    private calcControlPoint_Left(ex: number,ey: number): void {
        var w = ChangePage.PAGE_WIDTH;
        var h = ChangePage.PAGE_HEIGHT;
        ex = ex - this.dis.x;
        ey = ey - this.dis.y;

        if(ey <= h) {
            if(!this.inCircle(new egret.Point(ex,ey),new egret.Point(w,h),w)) {
                var insecp = this.getInsectionPoint(new egret.Point(ex,ey),new egret.Point(w,h),w);
                if(insecp) {
                    ex = insecp.x + w;
                    ey = insecp.y + h;
                } else {
                    return;
                }
            }
        } else {
            var r = Math.sqrt(w * w + h * h);
            if(!this.inCircle(new egret.Point(ex,ey),new egret.Point(w,0),r)) {
                var insecp = this.getInsectionPoint(new egret.Point(ex,ey),new egret.Point(w,0),r);
                if(insecp) {
                    ex = insecp.x + w;
                    ey = insecp.y;
                } else {
                    return;
                }
            }
        }
//        egret.Tween.get(this.tmpp,{
//            onChange: () => {
//                this.calcPointAndDraw_Left(this.tmpp.x,this.tmpp.y);
//            },onChangeObj: this
//        }).to({ x: ex,y: ey },200);
        this.calcPointAndDraw_Left(ex,ey);
    }
	
	private calcControlPoint_Right(ex:number, ey:number):void {
        var w = ChangePage.PAGE_WIDTH;
        var h = ChangePage.PAGE_HEIGHT;
        ex = ex - this.dis.x;
        ey = ey - this.dis.y;

        if(ey <= h) {
            if(!this.inCircle(new egret.Point(ex,ey),new egret.Point(0,h),w)) {
                var insecp = this.getInsectionPoint(new egret.Point(ex,ey),new egret.Point(0,h),w);
                ex = insecp.x;//+ w;
                ey = insecp.y + h;
            }
        } else {
            var r = Math.sqrt(w * w + h * h);
            if(!this.inCircle(new egret.Point(ex,ey),new egret.Point(0,0),r)) {
                var insecp = this.getInsectionPoint(new egret.Point(ex,ey),new egret.Point(0,0),r);
                ex = insecp.x;//+ w;
                ey = insecp.y;
                if(ex < -w) {
                    console.log("origin: ex="+ex+", ey="+ey);
                    var tx = h*ey*ex/((ex*ex)+(ey*ey));
                    ey = ey*tx/ex;
                    ex = tx;
                    console.log("pre: ex="+ex+", ey="+ey);
                    insecp = this.getInsectionPoint(new egret.Point(ex,ey),new egret.Point(0,h),r);
                    ex = insecp.x;//+ w;
                    ey = insecp.y;
                    console.log("ex="+ex+",ey="+ey)
                }
            }
        }
        
//        egret.Tween.get(this.tmpp,{onChange:()=>{
//            this.calcPointAndDraw_Right(this.tmpp.x,this.tmpp.y);
//            }, onChangeObj:this}).to({x:ex,y:ey}, 200);
        this.calcPointAndDraw_Right(ex,ey);
	}
	
	private calcPointAndDraw_Right(ex:number, ey:number):void {
        var result = this.calcPoint(ex,ey,this.bottom_right_point,ChangePage.PAGE_WIDTH,this.up_right_point,this.bottom_right_point);
        
        if(result == 1) {
            this.drawRightUp1();
            this.drawRightDown1();
        } else if(result == 2){
            this.drawRightUp2();
            this.drawRightDown2();
        }
	}
	
	private calcPointAndDraw_Left(ex:number, ey:number):void {
       
        var result = this.calcPoint(ex,ey,this.bottom_left_point,0,this.up_left_point,this.bottom_left_point);
        if(result == 1) {
            this.drawLeftUp1();
            this.drawLeftDown1();
        } else if(result == 2){
            this.drawLeftUp2();
            this.drawLeftDown2();
        }
	}
	//======================================求点方法===========================================================
	/**
	 * 获得点到圆心的线段与圆的交点
	 * 根据相似三角形
	 */ 
	private getInsectionPoint(p:egret.Point,cp:egret.Point,r:number):egret.Point {
        var disX = p.x - cp.x;
        var disY = p.y - cp.y;
        var R = Math.sqrt(disX * disX + disY * disY);
        if(R< 0.0001)return null;
        var x = r * disX / R;
        var y = Math.sqrt((r-x)*(r+x));
        if(disY < 0) {
            y = -y;
        }
        return new egret.Point(x,y);
	}
	
	/**
	 * 判断点是否在圆内
	 * (M.x - O.x)^2 + (M.y - O.y)^2 与 R*^2比较
	 * 大于：圆外
	 * 小于等于：圆内或圆上
	 */ 
	private inCircle(p:egret.Point, cp:egret.Point, r:number):boolean {
    	var disX = p.x - cp.x;
    	var disY = p.y - cp.y;
	  if(disX*disX + disY*disY <= r*r)
    	  return true;
    	else 
    	  return false;
	}
	
	/**
	 * 获得点相对于直线的对称点
	 * 直线 A*x + B*y + C = 0
	 * X = x + 2*A*((A*x+B*y+C)/(A*A+B*B))
	 * Y = y + 2*B*((A*x+B*y+C)/(A*A+B*B)) 
	 */
    private getSymmetryPoint(p: egret.Point,k: number,b: number): egret.Point {
        var C = (k * p.x - p.y + b) / ((k * k) + 1);
        return new egret.Point(p.x - ((k * C) << 1),p.y + (C << 1));
    }
	
    /**
     * 根据两点获取直线或线段旋转角度
     */ 
    private angle(target1: egret.Point,target2: egret.Point): number {
        var tmp_x: number = target1.x - target2.x;
        var tmp_y: number = target1.y - target2.y;
        var tmp_angle: number = Math.atan2(tmp_y,tmp_x) * 180 / Math.PI;
        tmp_angle = tmp_angle < 0 ? tmp_angle + 360 : tmp_angle;
        return tmp_angle;
    }
	
    /**
     * 计算各点
     * @param ex 计算好的在范围内的x坐标
     * @param ey 计算好的在范围内的y坐标
     * @param bottomp 底部坐标的点（往右翻是bottom_left_point,往左翻是bottom_right_point）
     * @param vertical_x_line 垂直于x轴的直线（往右翻是y=0,往左翻是y=w）
     * @param up_point topPoint关于直线的对称点
     */ 
	private calcPoint(ex:number, ey:number, bottomp:egret.Point, vertical_x_line:number, up_point:egret.Point, bottom_point:egret.Point):number {
        var w = ChangePage.PAGE_WIDTH;
        var h = ChangePage.PAGE_HEIGHT;
        
        if(Math.abs(ex - bottomp.x) < 0.00001)return 0;
        
        var bottom_with_control_k = ((ey - bottomp.y) / (ex - bottomp.x));
        if(Math.abs(bottom_with_control_k) < 0.00001)return 0;
        
        var perpendicular_to_bottom_with_control_k = -1.0 / bottom_with_control_k;
        if(Math.abs(perpendicular_to_bottom_with_control_k) < 0.00001) return 0;
        var perpendicular_to_bottom_with_control_b = (ey + bottomp.y) / 2 - perpendicular_to_bottom_with_control_k * ((ex + bottomp.x) / 2);
        
        var x_up_node:egret.Point = new egret.Point;
        var x_bottom_node: egret.Point = new egret.Point;
        var y_node: egret.Point = new egret.Point;
        
        x_up_node.x = -perpendicular_to_bottom_with_control_b / perpendicular_to_bottom_with_control_k;
        x_up_node.y = 0;
        
        x_bottom_node.x = (h - perpendicular_to_bottom_with_control_b) / perpendicular_to_bottom_with_control_k;
        x_bottom_node.y = h;
        
        y_node.x = vertical_x_line;
        y_node.y = perpendicular_to_bottom_with_control_b + perpendicular_to_bottom_with_control_k * vertical_x_line;
        
        this.controlPoint.x = ex;
        this.controlPoint.y = ey;
        this.bottom1Point.x = x_bottom_node.x;
        this.bottom1Point.y = x_bottom_node.y;
        if(this.bottom1Point.x > w) {
            this.bottom1Point.x = w;
        }
        
        if((ey <= h) && (y_node.y >= 0 && y_node.y <= h) ) {
            this.topPoint.x = y_node.x;
            this.topPoint.y = y_node.y;

            this.up1Point.x = 0;
            this.up1Point.y = 0;

            return 1;
        }
        var symp = this.getSymmetryPoint(bottom_point,perpendicular_to_bottom_with_control_k,perpendicular_to_bottom_with_control_b);
        this.topPoint.x = symp.x;
        this.topPoint.y = symp.y;

        this.up1Point.x = x_up_node.x;
        this.up1Point.y = x_up_node.y;
        return 2;
	}
	
	//======================================左边draw===========================================================
	private drawLeftUp1():void {
        var w = ChangePage.PAGE_WIDTH;
        var h = ChangePage.PAGE_HEIGHT;
    	  var ww = this.bottom1Point.x;
        var hh = h-this.topPoint.y;
        this.maskUp.graphics.clear();
        this.maskUp.graphics.beginFill(0,1);
        this.maskUp.graphics.lineStyle(1,0x00ff00,1);
        this.maskUp.graphics.moveTo(w,h);
        this.maskUp.graphics.lineTo(w,h-hh+1);
        this.maskUp.graphics.lineTo(w - ww+1,h);
        this.maskUp.graphics.endFill();
        
        this.up.rotation = this.angle(this.controlPoint, this.bottom1Point);
        this.up.x = this.controlPoint.x + this.down.x;
        this.up.y = this.controlPoint.y + this.down.y;
	}
	
	private drawLeftUp2():void {
        var w = ChangePage.PAGE_WIDTH;
        var h = ChangePage.PAGE_HEIGHT;
        var ww = this.bottom1Point.x;
        var hh = h;
        var disW = this.up1Point.x;
        this.maskUp.graphics.clear();
        this.maskUp.graphics.beginFill(0,1);
        this.maskUp.graphics.lineStyle(2,0x00ff00,1);
        this.maskUp.graphics.moveTo(w,h);
        this.maskUp.graphics.lineTo(w,h - hh);
        this.maskUp.graphics.lineTo(w - disW, h-hh);
        this.maskUp.graphics.lineTo(w - ww,h);
        this.maskUp.graphics.endFill();
    	
        var angle = this.angle(this.controlPoint,this.bottom1Point);
        this.up.rotation = angle;
        this.up.x = this.controlPoint.x + this.down.x;
        this.up.y = this.controlPoint.y + this.down.y;
	}
	
	private drawLeftDown1():void {
	    this.maskDown.graphics.clear();
        this.maskDown.graphics.beginFill(0,1);
        this.maskDown.graphics.moveTo(this.bottom1Point.x,this.bottom1Point.y);
        this.maskDown.graphics.lineTo(this.topPoint.x,this.topPoint.y);
        this.maskDown.graphics.lineTo(this.up1Point.x,this.up1Point.y);
        this.maskDown.graphics.lineTo(this.up2Point.x,this.up2Point.y);
        this.maskDown.graphics.lineTo(this.bottom2Point.x,this.bottom2Point.y);
        this.maskDown.graphics.endFill();
	}
	
    private drawLeftDown2(): void {
        this.maskDown.graphics.clear();
        this.maskDown.graphics.beginFill(0,1);
        this.maskDown.graphics.moveTo(this.bottom1Point.x,this.bottom1Point.y);
        this.maskDown.graphics.lineTo(this.up1Point.x,this.up1Point.y);
        this.maskDown.graphics.lineTo(this.up2Point.x,this.up2Point.y);
        this.maskDown.graphics.lineTo(this.bottom2Point.x,this.bottom2Point.y);
        this.maskDown.graphics.endFill();
    }
    
    //======================================右边draw===========================================================
    private drawRightUp1(): void {
        var w = ChangePage.PAGE_WIDTH;
        var h = ChangePage.PAGE_HEIGHT;
        var ww = this.bottom1Point.x;
        var hh = this.topPoint.y;
        this.maskUp.graphics.clear();
        this.maskUp.graphics.beginFill(0,1);
        this.maskUp.graphics.lineStyle(2,0x00ff00,1);
        this.maskUp.graphics.moveTo(0,h);
        this.maskUp.graphics.lineTo(0,hh);
        this.maskUp.graphics.lineTo(w - ww,h);
        this.maskUp.graphics.endFill();

        this.up.rotation = this.angle(this.controlPoint,this.bottom1Point) - 180;
        this.up.x = this.controlPoint.x + this.down.x;
        this.up.y = this.controlPoint.y + this.down.y;
    }

    private drawRightUp2(): void {
        var w = ChangePage.PAGE_WIDTH;
        var h = ChangePage.PAGE_HEIGHT;
        var ww = this.bottom1Point.x;
        var hh = h;
        var disW = this.up1Point.x;
        this.maskUp.graphics.clear();
        this.maskUp.graphics.beginFill(0,1);
        this.maskUp.graphics.lineStyle(2,0x00ff00,1);
        this.maskUp.graphics.moveTo(0,h);
        this.maskUp.graphics.lineTo(0,0);
        this.maskUp.graphics.lineTo(w - disW,0);
        this.maskUp.graphics.lineTo(w - ww,h);
        this.maskUp.graphics.endFill();

        var angle = this.angle(this.controlPoint,this.bottom1Point);
        this.up.rotation = angle - 180;
        this.up.x = this.controlPoint.x + this.down.x;
        this.up.y = this.controlPoint.y + this.down.y;
    }

    private drawRightDown1(): void {
        this.maskDown.graphics.clear();
        this.maskDown.graphics.beginFill(0,1);
        this.maskDown.graphics.moveTo(this.bottom1Point.x,this.bottom1Point.y);
        this.maskDown.graphics.lineTo(this.topPoint.x,this.topPoint.y);
        this.maskDown.graphics.lineTo(this.up2Point.x,this.up2Point.y);
        this.maskDown.graphics.lineTo(this.up_left_point.x,this.up_left_point.y);
        this.maskDown.graphics.lineTo(this.bottom_left_point.x,this.bottom_left_point.y);
        this.maskDown.graphics.endFill();
    }

    private drawRightDown2(): void {
        this.maskDown.graphics.clear();
        this.maskDown.graphics.beginFill(0,1);
        this.maskDown.graphics.moveTo(this.bottom1Point.x,this.bottom1Point.y);
        this.maskDown.graphics.lineTo(this.up1Point.x,this.up1Point.y);
        this.maskDown.graphics.lineTo(this.up_left_point.x,this.up_left_point.y);
        this.maskDown.graphics.lineTo(this.bottom_left_point.x,this.bottom_left_point.y);
        this.maskDown.graphics.endFill();
    }
    
    //=========================================================================================================
    public addPage(page:egret.DisplayObject):void {
        this.rightPages.push(page);
    }
    
    public get PageWidth():number {
        
        return 680;
    }
    
    public set PageWidth(value:number) {
        
    }
    
    public get PageHeight():number {
        
        return 1036;
    }
    
    public set PageHeight(value:number) {
        
    }
}
